package com.example.petcare.controller;

import com.example.petcare.pojo.HostDemand;
import com.example.petcare.service.HostService;
import com.example.petcare.service.PaymentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Controller
@CrossOrigin
@RequestMapping("/user/alipay")
public class PayController {



    private static final String OUT_TRADE_NO = "out_trade_no";// 商户订单号
    private static final String TRADE_SUCCESS = "TRADE_SUCCESS";// 交易成功标识
    private static final String TRADE_STATUS = "trade_status";// 交易状态
    private static final String TRADE_TIME = "gmt_payment";// 交易时间
    private static final String TRADE_NAME = "subject";// 交易名称
    private static final String TRADE_AMOUNT = "buyer_pay_amount";// 交易金额



    @Resource
    private PaymentService paymentService; // 注入支付服务类，用于处理支付请求

    @Autowired
    private HostService hostService;



    @ResponseBody// 返回json数据
    @PostMapping(value = "/create", produces = "text/html;charset=utf-8")
    public String create(@RequestParam("orderNo") String orderNo,
                         @RequestParam("orderName") String orderName,
                         @RequestParam("payPrice") String payPrice
                         ) {
        orderName="商品";
        return paymentService.create(orderNo, orderName, payPrice);// 创建支付宝订单
    }


    @GetMapping(value = "/success")
    public void success(@RequestParam Map<String, String> map, HttpServletResponse response) throws IOException {

        Integer hostId  = Integer.valueOf(map.get(OUT_TRADE_NO));// 商户订单号
//        System.out.println("hostId:"+hostId);

        // 先根据hostId查询托管需求
        HostDemand hostDemand = hostService.findById(hostId);
        // 如果查不到就不更新
        if (hostDemand == null) {
            response.sendRedirect("http://localhost:5173/user/host");
            return;
        }

        updateProductStatus(response, hostId);// 更新订单状态
    }

    // 更新订单状态
    private void updateProductStatus(HttpServletResponse response, Integer hostId) throws IOException {

        // 将订单状态改为已支付
        hostService.updatePayStatus(hostId);
        // 更新订单状态为已支付
        response.sendRedirect("http://localhost:5173/user/host");// 重定向到用户主页
    }

    // 支付宝异步通知
    @PostMapping(value = "/notify")
    public void payNotify(@RequestParam Map<String, String> map) {
        if (TRADE_SUCCESS.equals(map.get(TRADE_STATUS))) // 交易成功
        {
            String payTime = map.get(TRADE_TIME);// 交易时间
            String tradeNo = map.get(OUT_TRADE_NO);// 商户订单号
            String tradeName = map.get(TRADE_NAME);// 交易名称
            String payAmount = map.get(TRADE_AMOUNT);// 交易金额

            log.info("[支付成功] {交易时间：{},订单号：{},订单名称：{},交易金额：{}}", payTime, tradeNo, tradeName, payAmount);
        }


    }






}
